﻿using System.Reflection;

namespace JustinAngel.Neurons.Framework.Helpers
{
    public static class ReflectionHelper
    {
        public static string GetAssemblyName(this Assembly assembly)
        {
            return assembly.FullName.Substring(0, assembly.FullName.IndexOf(","));
        }

        public static object GetPropertyByName(this object obj, string propertyName)
        {
            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                if (propertyInfo.CanRead && propertyInfo.Name == propertyName)
                    return propertyInfo.GetValue(obj, new object[0]);
            }
            return null;
        }

        public static bool HasProperty(this object obj, string propertyName)
        {
            return obj.GetType().GetProperty(propertyName) != null;
        }
    }
}
